//
//  PLPlatform.h
//  Plankton
//
//  Created by Marc Schlichte on 2008-07-12.
//  Copyright 2008-2009 Framework Labs. All rights reserved.
//

#import <Foundation/Foundation.h>

#import "PLEndpoint.h"	// for endpoint delegate

@class PLPattern;

/// This is the main entity of Plankton. It allows users to send and receive messages
/// to and from other instances running locally or remotely. A user of plankton is identified
/// by an actor-id (aid) which can be requested from a platform.
///
/// Messages transmitted must be valid plist objects. Reception of messages is done by registereing
/// pattern objects, which scan the incomming message queue for matching messages. Matching patterns are
/// removed after dispatching the message to them.
///
@interface PLPlatform : NSObject <PLEndpointDelegate>
{
@private
    PLEndpoint			*endpoint;
    NSUInteger			nextLocalAid;
    NSMutableDictionary	*patterns;
    NSMutableArray		*messages;
    unsigned long long	nextTag; 
}

- (NSString *)makeAid;
- (void)destroyAid:(NSString *)aid;
+ (int)portOfAid:(NSString *)aid;

- (void)send:(id)message to:(NSString *)aid;
- (void)multicast:(id)message to:(id<NSFastEnumeration>)aids;

- (void)receive:(PLPattern *)pattern by:(NSString *)aid;
- (void)cancelReceive:(PLPattern *)pattern;
- (BOOL)isReceiving:(PLPattern *)pattern;

#if NS_BLOCKS_AVAILABLE
- (void)receive:(NSPredicate *)predicate by:(NSString *)aid 
	  withBlock:(void (^)(id message, PLPattern *))messageBlock
   timeoutBlock:(void (^)(PLPattern *))timeoutBlock after:(NSTimeInterval)timeoutInterval;
#endif

- (NSNumber *)createTag;

@end

